package com.humble.humbleojquestionservice.controller.inner;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.humble.humbleojmodel.model.entity.Question;
import com.humble.humbleojmodel.model.entity.QuestionSubmit;
import com.humble.humbleojquestionservice.service.QuestionService;
import com.humble.humbleojquestionservice.service.QuestionSubmitService;
import com.humble.humbleojserviceclient.service.QuestionFeignClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;

@RestController
@RequestMapping("/inner")
public class QuestionInnerController implements QuestionFeignClient {
    @Resource
    private QuestionService questionService;

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Override
    @GetMapping("/get/id")
    public Question getById(@RequestParam("id") Serializable id) {
        return questionService.getById(id);
    }

    @Override
    @PostMapping("/update")
    public boolean update(@RequestBody Question question) {
        UpdateWrapper<Question> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",question.getId());
        if(question.getSubmitNum()!=null&&question.getSubmitNum()>0) {
            updateWrapper.set("submitNum", question.getSubmitNum());
        }
        if(question.getAcceptedNum()!=null&&question.getAcceptedNum()>0) {
            updateWrapper.set("acceptedNum", question.getAcceptedNum());
        }
        return questionService.update(updateWrapper);
    }

    @Override
    @GetMapping("/submit/get/id")
    public QuestionSubmit getSubmitById(@RequestParam("id") Serializable id){
        return questionSubmitService.getById(id);
    }
    @Override
    @PostMapping("/submit/update")
    public boolean updateSubmit(@RequestBody QuestionSubmit questionSubmit){
        UpdateWrapper<QuestionSubmit> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",questionSubmit.getId());
        if(questionSubmit.getStatus()!=null&&questionSubmit.getStatus()!=0) {
            updateWrapper.set("status", questionSubmit.getStatus());
        }
        if(questionSubmit.getJudgeInfo()!=null&& !questionSubmit.getJudgeInfo().isEmpty()) {
            updateWrapper.set("judgeInfo", questionSubmit.getJudgeInfo());
        }
        return questionSubmitService.update(updateWrapper);
    }
}
